package com.wnxy.mallserver.exception.handler;

import com.wnxy.mallserver.exception.AccountOrPasswordException;
import com.wnxy.mallserver.exception.GoodsStockException;
import com.wnxy.mallserver.response.ResponseResult;
import com.wnxy.mallserver.response.StateCode;
import com.wnxy.mallserver.vo.goods.GoodsStockExceptionVo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice//controller通知类，利用AOP切面
public class GlobalExceptionHandler {

    //捕获登录异常
    @ExceptionHandler(AccountOrPasswordException.class)
    public ResponseResult<Object> handler(AccountOrPasswordException e){
        e.printStackTrace();
        return new ResponseResult<>(StateCode.FAIL,e.getMessage(),null);
    }

    //捕获   商品库存不足  的异常
    @ExceptionHandler(GoodsStockException.class)//反射的原理
    public ResponseResult<GoodsStockExceptionVo> handleGoodsStockException(GoodsStockException e){
        e.printStackTrace();
        return new ResponseResult<>(StateCode.FAIL,e.getMessage(),e.getGoodsStockExceptionVo());
    }
//
//    /**捕获权限异常，并返回给前端*/
//    @ExceptionHandler(PermsException.class)
//    public ResponseResult<String> handleGoodsPermsException(PermsException e){
//        e.printStackTrace();
//        return new ResponseResult<>(StateCode.NO_PERMS,e.getMessage(),null);
//    }
//
//    /**捕获security的异常*/
//    @ExceptionHandler({
//            BadCredentialsException.class,
//            InternalAuthenticationServiceException.class })//捕获登录和数据校验异常
//    public ResponseResult<Object> securityHandler(Exception e){
//        e.printStackTrace();
//        return new ResponseResult<>(StateCode.FAIL, Message.ACCOUNT_OR_PASSWORD_ERROR,null);
//    }
}

























